#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[12][10];
int sum[12];
int flag[12];
int main()
{
//	a[0]={0};
	sum[0]=0;
	flag[0]=1;
	a[1]={1,2,3,4,5,6,7,8,9,0};
	sum[1]=45;
	flag[1]=10;
	a[2]={2,4,6,8,0};
	sum[2]=20;
	flag[2]=5;
	a[3]={3,5,9,2,5,8,1,4,7,0};
	sum[3]=45;
	flag[3]=10;
	a[4]={4,8,2,6,0};
	sum[4]=20;
	flag[4]=5;
	a[5]={5,0};
	sum[5]=5;
	flag[5]=2;
	a[6]={6,2,8,4,0};
	sum[6]=20;
	flag[6]=5;
	a[7]={7,4,1,8,5,2,9,6,3,0};
	sum[7]=45;
	flag[7]=10;
	a[8]={8,6,4,2,0};
	sum[8]=20;
	flag[8]=5;
	a[9]={9,8,7,6,5,4,3,2,1,0};
	sum[9]=45;
	flag[9]=10;
	int q;
	scanf("%d",&q);
	while(q--)
	{
		unsigned long long ans=0;
		unsigned long long n,m;
		scanf("%lld %lld",&n,&m);
		unsigned long long k = n/m;
		int t = n%10;
		if(k>0)
		{
			long long cnt = k/flag[t];
			long long sign = k%flag[t];
			ans = cnt*sum[t];
			for(int i=0;i<sign;i++) ans += a[i];
		}
		printf("%lld\n",ans);
		 
	}
}
